/**
 * 基础功能相关接口
 */
import http, { BASE_NAME, ResponseSuccessCode } from './index';

export const apiBasePath_system_permission = `${BASE_NAME}/api/system_permission`;
export const apiBasePath_system_role = `${BASE_NAME}/api/system_role`;
export const apiBasePath_system_user = `${BASE_NAME}/api/system_user`;
export const apiBasePath_system_dept = `${BASE_NAME}/api/system_dept`;
export const apiBasePath_system_duty_dept = `${BASE_NAME}/api/system_duty_dept`;
export const apiBasePath_system_log = `${BASE_NAME}/api/operationLog`;
export const apiBasePath_system_layer = `${BASE_NAME}/api/map_service`;
export const apiBasePath_system_route = `${BASE_NAME}/api/patrol_route`;
export const apiBasePath_system_weather = `${BASE_NAME}/v7/weather/now`;

/**
 * 获取所有权限（菜单）
 */
export async function getSystemPermissionAll() {
  try {
    const response = await http.get(apiBasePath_system_permission);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建权限（菜单）
 */
export async function postSystemPermission(data) {
  try {
    const response = await http.post(apiBasePath_system_permission, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑权限（菜单）
 */
export async function putSystemPermission(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_permission}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除权限（菜单）
 */
export async function delSystemPermission(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_permission}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取所有角色
 */
export async function getSystemRoleAll() {
  try {
    const response = await http.get(apiBasePath_system_role);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建角色
 */
export async function postSystemRole(data) {
  try {
    const response = await http.post(apiBasePath_system_role, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑角色
 */
export async function putSystemRole(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_role}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除角色
 */
export async function delSystemRole(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_role}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取用户列表
 */
export async function getSystemUserPage(data) {
  try {
    const response = await http.get(`${apiBasePath_system_user}/page`, {
      params: data,
    });

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 查询用户详情
 */
export async function getSystemUserDetail(id) {
  try {
    const response = await http.get(`${apiBasePath_system_user}/${id}`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建用户
 */
export async function postSystemUser(data) {
  try {
    const response = await http.post(apiBasePath_system_user, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑用户
 */
export async function putSystemUser(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 重置密码
 */
export async function putSystemUserRestPass(id) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/${id}/reset_password`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除用户
 */
export async function delSystemUser(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_user}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 修改我的信息
 */
export async function reviseMineInfo(data) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/mine`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 修改个人密码
 */
export async function reviseMinePassword(data) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/password`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取所有组织
 */
export async function getSystemDeptAll(type, deptId) {
  try {
    const response = await http.get(`${apiBasePath_system_dept}?type=${type ?? ''}&deptId=${deptId ?? ''}`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取所有组织(包含用户)
 */
export async function getSystemDeptAllUserTree(data) {
  try {
    const response = await http.get(`${apiBasePath_system_dept}/user_tree`, {
      params: data,
    });

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 查询用户责任区域
 */
export async function getSystemDutyDeptUser(userId) {
  try {
    const response = await http.get(`${apiBasePath_system_duty_dept}/user/${userId}`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建组织
 */
export async function postSystemDept(data) {
  try {
    const response = await http.post(apiBasePath_system_dept, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑组织
 */
export async function putSystemDept(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_dept}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除组织
 */
export async function delSystemDept(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_dept}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 分页查询日志
 */
export async function postSystemLogPage(data) {
  try {
    const response = await http.post(`${apiBasePath_system_log}/pageQueryLog`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除日志
 */
export async function delSystemLog(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_log}/batchDeleteLog/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 目录关联地图服务
 */
export async function postSystemLayerRelation(data) {
  try {
    const response = await http.post(`${apiBasePath_system_layer}/relation`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 查询已发布的列表
 */
export async function getSystemLayerRelationList() {
  try {
    const response = await http.get(`${apiBasePath_system_layer}/list`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 根据目录id查询关联的服务
 */
export async function getSystemLayerRelation(data) {
  try {
    const response = await http.get(`${apiBasePath_system_layer}/relation`, {
      params: data,
    });

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 和风天气API
 */

export async function getWeatherApi() {
  try {
    const response = await http.get(
      `http://61.178.220.130:7080/patrol/qweather/weather?path=%2Fv7%2Fweather%2Fnow&param=location%3D105.81611,38.866%26gzip%3Dn%26key%3D019632898663445489b4a0eff5df0c12`,
    );

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 和风天气预警API
 */

export async function getWeather3dApi() {
  try {
    const response = await http.get(
      `http://61.178.220.130:7080/patrol/qweather/weather?path=%2Fv7%2Fweather%2F3d&param=location%3D105.81611,38.866%26gzip%3Dn%26key%3D019632898663445489b4a0eff5df0c12`,
    );

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 和风天气灾害预警API
 */

export async function getWeatherWarnNowApi() {
  try {
    const response = await http.get(
      `http://61.178.220.130:7080/patrol/qweather/weather?path=%2Fv7%2Fwarning%2Fnow&param=location%3D105.81611,38.866%26gzip%3Dn%26key%3D019632898663445489b4a0eff5df0c12`,
    );

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 添加巡护路线
 */
export async function postSystemRouteListAdd(data) {
  try {
    const response = await http.post(`${apiBasePath_system_route}/add`, data);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 修改巡护路线
 */
export async function postSystemRouteUpdate(data) {
  try {
    const response = await http.post(`${apiBasePath_system_route}/update`, data);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 巡护路线查询
 */
export async function getSystemRouteList(data) {
  try {
    const response = await http.get(
      `${apiBasePath_system_route}/list?deptId=${data?.deptId || ''}&type=${data?.type || ''}`,
    );

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}
/**
 * 删除巡护路线
 */

export async function delSystemRoute(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_route}/delete?ids=${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 查询所有图层类型
 */
export async function getSystemLayerCategoryList() {
  try {
    const response = await http.get(`${BASE_NAME}/api/system_layer_category/list`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 新增图层类型
 */
export async function postSystemLayerCategoryList(data) {
  try {
    const response = await http.post(`${BASE_NAME}/api/system_layer_category`, data);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 查询图层列表
 */
export async function getSystemLayerCategoryConfig(category) {
  try {
    const response = await http.get(`${BASE_NAME}/api/system_layer_config?category=${category?.category}`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 新增图层
 */
export async function postSystemLayerConfig(data) {
  try {
    const response = await http.post(`${BASE_NAME}/api/system_layer_config`, data);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑图层
 */
export async function putSystemLayerConfig(id, data) {
  try {
    const response = await http.put(`${BASE_NAME}/api/system_layer_config/${id}`, data);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除图层
 */

export async function delSystemLayerConfig(id) {
  try {
    const response = await http.delete(`${BASE_NAME}/api/system_layer_config/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除图层分类
 */

export async function delSystemLayerCategory(id) {
  try {
    const response = await http.delete(`${BASE_NAME}/api/system_layer_category/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}
